package com.demo.util;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class RequestUtil 
{

	/**
	 * 通过key获取值
	 * @param key
	 * @return
	 */
	public static String getValue( String key) {

		String value = null;
		HttpServletRequest request = getRequest();
		if(request != null)
			value = request.getParameter(key);
		return value;
	}
	
	/**
	 * 获取 request
	 * @return
	 */
	public static HttpServletRequest getRequest()
	{
		HttpServletRequest request = null;
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		if(requestAttributes != null)
		{
			ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
			request = servletRequestAttributes.getRequest();
		}
		return request;
	}
	
	/**
	 * 获取request的parameterMap
	 * @return Map<String , String>
	 */
	public static Map<String , String> getParameterMap()
	{
		HttpServletRequest request = getRequest();
		Map<String , String> paramMap = new HashMap<String , String>();
		if(request != null)
		{
			Map<String , String[]> map = request.getParameterMap();
			Iterator<Entry<String , String[]>> iterator = map.entrySet().iterator();
			while(iterator.hasNext())
			{
				Entry<String , String[]> entry = iterator.next();
				String key = entry.getKey();
				String value = "";
				Object valueObj = entry.getValue();
				if (valueObj instanceof String[])
				{
					String[] values = (String[]) valueObj;
					for (int i = 0; i < values.length; i++)
					{
						value = values[i] + ",";
					}
					value = value.substring(0, value.length() - 1);
				}
				paramMap.put(key,  value);
			}
		}
		return paramMap;

	}
	/**
	 * 是否是Ajax请求
	 * 
	 * @param request
	 * @return
	 */
	public static boolean isAjax(ServletRequest request)
	{
		return "XMLHttpRequest".equalsIgnoreCase(((HttpServletRequest) request).getHeader("X-Requested-With"));
	}
	
	
	/**
	 * 获取ip地址
	 * @param request
	 * @return
	 */
	public static String getIpAddr(HttpServletRequest request)
	{
		String ip = request.getHeader("Real-IP");
		if (isNullOrEmpty(ip) || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getHeader("X-Real-IP");
		}
		if (isNullOrEmpty(ip) || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getHeader("X-Forwarded-For");
		}
		if (!isNullOrEmpty(ip) && !"unknown".equalsIgnoreCase(ip))
		{
			// 多次反向代理后会有多个IP值，第一个为真实IP。
			int index = ip.indexOf(',');
			if (index != -1)
			{
				return ip.substring(0, index);
			}
			return ip;

		}
		return request.getRemoteAddr();
	}
	
	/***
	 * 判断对象是否为空
	 * @param obj
	 * @return
	 */
	private static boolean isNullOrEmpty(Object obj)
	{
		try
		{
			if (obj == null)
				return true;
			if (obj instanceof CharSequence)
			{
				return ((CharSequence) obj).length() == 0;
			}
			if (obj instanceof Collection)
			{
				return ((Collection) obj).isEmpty();
			}
			if (obj instanceof Map)
			{
				return ((Map) obj).isEmpty();
			}
			if (obj instanceof Object[])
			{
				Object[] object = (Object[]) obj;
				if (object.length == 0)
				{
					return true;
				}
				boolean empty = true;
				for (int i = 0; i < object.length; i++)
				{
					if (!isNullOrEmpty(object[i]))
					{
						empty = false;
						break;
					}
				}
				return empty;
			}
			return false;
		} catch (Exception e)
		{
			return true;
		}

	}
}